import request from '@/utils/request'

/* 上报错误信息 */
export function logError(data, showLoading = false) {
  return request({
    url: 'https://oapi.dingtalk.com/robot/send?access_token=9f44ad3005cf5d10a6d19bb8e4c36da28103587030c40469d74b20a57a574211',
    method: 'post',
    data,
    headers: {
      showLoading
    }
  })
}

export function refreshToken(params = {}, showLoading = false) {
  return request({
    url: '/token/refresh',
    method: 'get',
    params,
    headers: {
      showLoading
    }
  })
}

/**
 * @Author: wjk
 * @Date: 2019-12-10 14:51:34
 * @LastEditors: wjk
 * @description: 上传图片接口
 * @param {count} Number	否	最多可以选择的图片张数，默认9	见下方说明
 * @param {sizeType} Array<String>	否	original 原图，compressed 压缩图，默认二者都有	5+App、微信小程序、支付宝小程序、百度小程序
 * @param {sourceType} 	Array<String>	否	album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
 * @return: 图片url
 */
export function uploadImg(option = {}) {
  return new Promise((resolve, reject) => {
    const type = option.type || 'chooseImage'
    // const videoUrl = 'https://bxuatapi.bisinuolan.cn/api/bsnl-oss/appUploadShot'
    // const imageUrl = process.env.VUE_APP_MINI_API + '/bsnl-oss/appUpload'
    const uploadUrl = `${process.env.VUE_APP_MINI_API}/bsnl-oss/appUploadShot`
    let flag = false
    uni[type]({
      ...option,
      success: chooseImageRes => {
        let tempFilePaths = chooseImageRes.tempFilePaths
        if (type === 'chooseVideo') {
          if (option.limit && chooseImageRes.size > 1024 * 1024 * 25) {
            uni.showToast({
              title: '视频超过25M，请压缩后重新上传',
              icon: 'none',
              duration: 2000
            })
            return
          }
          flag = 1
          tempFilePaths = [chooseImageRes.tempFilePath]
        } else if (option.count) {
          tempFilePaths = chooseImageRes.tempFilePaths.slice(0, option.count)
          var limit = -1
          if (option.limit) {
            chooseImageRes.tempFiles.map((item, index) => {
              if (item.size > 1024 * 1024 * 5) {
                limit = index
              }
            })
            if (limit > -1) {
              tempFilePaths.splice(limit, 1)
            }
          }
          flag = 0
        }
        if (!tempFilePaths.length && limit > -1) {
          uni.showToast({
            title: '图片超过5M，请压缩后重新上传',
            icon: 'none',
            duration: 2000
          })
        }
        if (!tempFilePaths.length) return
        const tempArr = []
        uni.showLoading({
          title: '上传中'
        })
        tempFilePaths.forEach((item, i) => {
          uni.uploadFile({
            url: uploadUrl,
            filePath: item,
            name: 'file',
            formData: {
              user: '',
              shot: flag,
              info: option.info || 0
            },
            success: uploadFileRes => {
              const res = JSON.parse(uploadFileRes.data || '', flag)
              if (res && res.code === 0 && res.data) {
                if (res.data.snapShot) {
                  tempArr.push({
                    sort: i,
                    url: res.data.filePath,
                    cover: res.data.snapShot,
                    imgInfo: res.data.imgInfo
                  })
                } else {
                  tempArr.push({
                    sort: i,
                    url: res.data.filePath,
                    imgInfo: res.data.imgInfo
                  })
                }
              } else {
                tempArr.push({
                  sort: i,
                  url: '',
                  imgInfo: null
                })
              }
            },
            fail: () => {
              tempArr.push({
                sort: i,
                url: '',
                imgInfo: null
              })
            },
            complete: () => {
              if (tempArr.length === tempFilePaths.length) {
                uni.hideLoading()
                if (limit > -1) {
                  uni.showToast({
                    title: '图片超过5M，请压缩后重新上传',
                    icon: 'none',
                    duration: 2000
                  })
                }
                resolve({
                  success: tempArr.some(i => i.url),
                  url: tempArr.filter(i => i.url)
                })
              }
            }
          })
        })
      }
    })
  })
}

/* 获取引导页信息 */
export function queryLaunch(params = {}, showLoading = true) {
  return request({
    url: '/launch',
    method: 'get',
    params,
    headers: {
      showLoading,
      showMessage: false
    }
  })
}

/*
获取小程序二维码
http://127.0.0.1:8000/api/mini-program/mini-code-url?page=pages/home/index&scene=a=21&width=430
*/
export function queryMiniCode(data = {}, showLoading = true) {
  return request({
    url: '/mini-program/mini-code-url',
    method: 'post',
    data,
    headers: {
      showLoading
    }
  })
}

/*
解析小程序二维码
http://127.0.0.1:8000/api/mini-program/share-code?shareCodeId=5792759
*/
export function getMiniCodeParams(data = {}, showLoading = true) {
  return request({
    url: '/mini-program/share-code',
    method: 'post',
    data,
    headers: {
      showLoading
    }
  })
}
